<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="axios.js"></script>
    <script>
      const request = axios.create({
        baseURL: 'http://localhost:8080', //主机地址
        timeout: 1000, //超越时间 毫秒
        // headers: {
        //   token: 'helen123',
        // },
      })
      // 请求拦截器
      request.interceptors.request.use(
        function (config) {
          console.log('请求拦截')
          console.log('config', config)
          config.headers.mytoken = 'helen123'
          return config
        },
        function (error) {
          return Promise.reject('error', error)
        }
      )
      //添加响应拦截器
      request.interceptors.request.use(
        function (response) {
          console.log('response', response)
          return response
        },
        function (error) {
          //对错误做点什么
          //......
          return Promise.reject(error)
        }
      )

      request({
        url: '/user/list',
        method: 'get',
      })
        .then((response) => {
          console.log('数据获取成功', response)
        })
        .catch((error) => {
          console.log('数据获取失败', error)
        })

      //另一种写法
      request
        .get('/user/list')
        .then((response) => {
          console.log('数据获取成功', response.data)
        })
        .catch((error) => {
          console.log('数据获取失败', error)
        })
    </script>
  </body>
</html>
